package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.OrderQuantityAttrHandler;
import com.iwhalecloud.bss.kite.cucc.service.goods.GoodsHelper;
import com.iwhalecloud.bss.kite.cucc.spec.impl.transfer.GoodsInfoTransfer;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Offer;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@AttrHandler(OrderQuantityAttrHandler.class)
public class OrderQuantityAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    private static final String ORDER_QUANTITY_ATTR = "200001696";

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        // 不是框架的主产品不处理
        KiteOfferInst mainOfferInst = querySceneInstService.getGiftOfferInst(data.getSceneInstId());
        if (Objects.isNull(mainOfferInst)) {
            return;
        }
        if (!GoodsHelper.isGift(mainOfferInst.getOfferId())) {
            return;
        }
        // 框架主产品的订购数量
//        KiteAttr orderQuantityAttr = inst.getAttr(ORDER_QUANTITY_ATTR);
//        if (Objects.nonNull(orderQuantityAttr)) {
//            // 获取礼包下的所有主套餐
//            List<KiteOfferInst> kiteOfferInstList = querySceneInstService.listMainOfferInstsFromGiftOfferInst(data.getCustId(), mainOfferInst.getOfferInstId());
//            if (KiteListUtils.isNotEmpty(kiteOfferInstList)) {
//                kiteOfferInstList.forEach(kiteOfferInst -> {
//                    // 获取该主套餐下的所有接入产品
//                    List<KiteProdInst> prodInstList = querySceneInstService.listProdInstsFromOfferInst(data.getCustId(), kiteOfferInst.getOfferInstId());
//                    if (KiteListUtils.isNotEmpty(prodInstList)) {
//                        prodInstList.forEach(kiteProdInst -> {
//                            KiteAttr subOrderQuantityAttr = kiteProdInst.getAttr(ORDER_QUANTITY_ATTR);
//                            if (Objects.nonNull(subOrderQuantityAttr)) {
//                                subOrderQuantityAttr.setValue(orderQuantityAttr.getValue());
//                                subOrderQuantityAttr.setValueDesc(orderQuantityAttr.getValueDesc());
//                                operateSceneInstService.setAttr(data.getSceneInstId(), kiteProdInst.getInstType(), kiteProdInst.getInstId(), subOrderQuantityAttr);
//                            }
//                        });
//                    }
//                });
//            }
//        }

        //设置全部成员产品、附加产品订购数量
        linkAdditionalProducts(inst,data);

    }
/**
 * 设置附加产品订购数量
 */
private void linkAdditionalProducts(KiteInst inst, AttrData data) {
    String offerInstId = querySceneInstService.getSceneInst(data.getSceneInstId()).getRootInstId();
    // 框架主产品的订购数量
    KiteAttr orderQuantityAttr = inst.getAttr(ORDER_QUANTITY_ATTR);
    List<KiteProdInst> kiteProdInsts = querySceneInstService.listProdInstsFromGiftOfferInst(data.getCustId(), offerInstId);
    if (KiteListUtils.isNotEmpty(kiteProdInsts)) {
        kiteProdInsts.forEach(kiteProdInst -> {
            KiteAttr subOrderQuantityAttr = kiteProdInst.getAttr(ORDER_QUANTITY_ATTR);
            if (Objects.nonNull(subOrderQuantityAttr)) {
                subOrderQuantityAttr.setValue(orderQuantityAttr.getValue());
                subOrderQuantityAttr.setValueDesc(orderQuantityAttr.getValueDesc());
                operateSceneInstService.setAttr(data.getSceneInstId(), kiteProdInst.getInstType(), kiteProdInst.getInstId(), subOrderQuantityAttr);
            }
        });
    }
}


    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
